package com.aiju.cash.widgit;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;


/**
 * @author: 佐为
 * @date: 2017-04-12 14:24
 * @version: V1.0
 * @Description: 限制输入两位小数的EditView
 */
public class TwoPointEditView extends EditText {

    public TwoPointEditView(Context context) {
        super(context);
        initView();
    }

    public TwoPointEditView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        setMaxLines(1);
        setBackgroundColor(Color.TRANSPARENT);
        setHintTextColor(Color.parseColor("#bbbbbb"));
        setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);
        addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable edt) {
                String temp = edt.toString();
                int posDot = temp.indexOf(".");
                if (posDot <= 0) return;
                if (temp.length() - posDot - 1 > 2) {
                    edt.delete(posDot + 3, posDot + 4);
                }
            }

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }
        });
    }

}
